// Fill out your copyright notice in the Description page of Project Settings.


#include "SRingGraphList.h"
#include "Widgets/Images/SImage.h"

#include "Widgets/Layout/SSpacer.h"
#include "ListLibrary/BaseWidget/STextBlock_TextStyleLambda_List.h"
#include "ListLibrary/ListUIStyle.h"
#include "SlateOptMacros.h"
#include "ListLibrary/GlobalConfig.h"
#include "Widgets/Colors/SColorBlock.h"

BEGIN_SLATE_FUNCTION_BUILD_OPTIMIZATION

void SRingGraphList::Construct(const FArguments& InArgs)
{
	ShowInfo = SNew(SVerticalBox).Visibility_Lambda([this]()
	{
		if(ShowTextInfo)
			return EVisibility::Visible;
		else
			return EVisibility::Collapsed;
	});
	ChildSlot
	[
		SNew(SVerticalBox)
		+ SVerticalBox::Slot().VAlign(VAlign_Top)
		[
			SNew(SHorizontalBox)
			+ SHorizontalBox::Slot().AutoWidth()
			[
				SNew(SBox)
				[
					SNew(SOverlay)
					+ SOverlay::Slot().HAlign(HAlign_Fill).VAlign(VAlign_Fill)
					[
						SAssignNew(WaiYuan,SImage)
						.SETIMAGE("WaiYuan")
					]
					+ SOverlay::Slot().HAlign(HAlign_Center).VAlign(VAlign_Center)
					[
						SAssignNew(NeiYuan,SImage)
						.SETIMAGE("NeiYUan")
					]
					+ SOverlay::Slot().HAlign(HAlign_Center).VAlign(VAlign_Center)
					[
						SNew(SVerticalBox)
						+ SVerticalBox::Slot().AutoHeight()
						[
							SAssignNew(AllData, STextBlock_TextStyleLambda_List)
							.Justification(ETextJustify::Center)
							.Text(FText::FromString(TEXT("")))
							.TEXTSTYLE("AlibabaBold_18_FFFFFFD9")
						]
						+ SVerticalBox::Slot().AutoHeight()
						[
							SAssignNew(OtherText,STextBlock_TextStyleLambda_List)
							.Justification(ETextJustify::Center)
							.Text(FText::FromString(TEXT("总工程量")))
							.TEXTSTYLE("AlibabaRegular_10_FFFFFFD9")
						]
					]

				]
			]
			+ SHorizontalBox::Slot().FillWidth(1).Padding(FMargin(24, 0, 0, 0))
			[
				ShowInfo.ToSharedRef()
			]
		]
	];
}


END_SLATE_FUNCTION_BUILD_OPTIMIZATION

SRingGraphList::SRingGraphList()
{
	InsideDiameter = 52;
	LoopWidth = 21;
	CenterX = 81;
	CenterY = 81;
	//深蓝
	ColorS.Add(FLinearColor(0.007, 0.32, 1, 1));
	//青色
	ColorS.Add(FLinearColor(0.034, 1, 0.52, 1));
	//白色
	ColorS.Add(FLinearColor(0.63, 0.73, 0.85, 1));
	//黄色
	ColorS.Add(FLinearColor(0.93, 0.46, 0.0, 1));
	//绿色
	ColorS.Add(FLinearColor(0.15, 0.66, 0.0, 1));
	//淡蓝色
	ColorS.Add(FLinearColor(0.13, 0.75, 0.743, 1));
	//紫色
	ColorS.Add(FLinearColor(0.12, 0.036, 1, 1));
	//深粉
	ColorS.Add(FLinearColor(0.46, 0.02, 0.75, 1));
	//橘色
	ColorS.Add(FLinearColor(0.96, 0.13, 0.0, 1));
	//深蓝
	ColorS.Add(FLinearColor(0.03, 0.56, 1, 1));

	TextColor.Add("AlibabaRegular_10_159AFFD9");
	TextColor.Add("AlibabaRegular_10_34FFBFD9");
	TextColor.Add("AlibabaRegular_10_D0DEEED9");
	TextColor.Add("AlibabaRegular_10_F7B500D9");
	TextColor.Add("AlibabaRegular_10_6DD400D9");
	TextColor.Add("AlibabaRegular_10_66E1DFD9");
	TextColor.Add("AlibabaRegular_10_6236FFD9");
	TextColor.Add("AlibabaRegular_10_B620E0D9");
	TextColor.Add("AlibabaRegular_10_FA6400D9");
	TextColor.Add("AlibabaRegular_10_32C5FFD9");
	ShowTextInfo = true;
	Definition = 0.5;
}


int32 SRingGraphList::OnPaint(const FPaintArgs& Args, const FGeometry& AllottedGeometry, const FSlateRect& MyCullingRect, FSlateWindowElementList& OutDrawElements, int32 LayerId, const FWidgetStyle& InWidgetStyle, bool bParentEnabled) const
{
	if (ListData.Num() > 0)
	{
		float BeginJIaodu = 0.0;
		int Coloritem = 0;
		for (auto& item : ListData)
		{
			if (item.Value <= 0.0)
				continue;
			for (float i = 0.0; i <= item.Value; i += Definition)
			{
				TArray<FVector2D> points;
				float jiaodu = i + BeginJIaodu;
				if(jiaodu >= 360.0)
					jiaodu -= 360.0;
				float ang = M_PI * (jiaodu / 180.0); //计算当前的角度

				float radius = InsideDiameter;
				FVector2D Point;
				GetEllipseOuterPoint(CenterX,CenterY,radius,radius,ang,Point);
				points.Add(Point);
				radius = InsideDiameter + LoopWidth;
				GetEllipseOuterPoint(CenterX,CenterY,radius,radius,ang,Point);
				points.Add(Point);

				FSlateDrawElement::MakeLines( //绘制线条
					OutDrawElements,
					LayerId++,
					AllottedGeometry.ToPaintGeometry(),
					points, //线条坐标
					bParentEnabled ? ESlateDrawEffect::None : ESlateDrawEffect::DisabledEffect, //在原色基础之上在增加别的效果
					ColorS[Coloritem], //显示颜色
					true, //抗锯齿
					3 //线的宽度
				);
			}
			BeginJIaodu += item.Value;
			Coloritem = (Coloritem + 1) % 10;
		}
	}
	else
	{
		for (float jiaodu = 0.0; jiaodu <= 360.0; jiaodu += Definition)
		{
			TArray<FVector2D> points;
			float ang = M_PI * (jiaodu / 180.0); //计算当前的角度

			float radius = InsideDiameter;
			FVector2D Point;
			GetEllipseOuterPoint(CenterX,CenterY,radius,radius,ang,Point);
			points.Add(Point);
			radius = InsideDiameter + LoopWidth;
			GetEllipseOuterPoint(CenterX,CenterY,radius,radius,ang,Point);
			points.Add(Point);

			FSlateDrawElement::MakeLines( //绘制线条
				OutDrawElements,
				LayerId++,
				AllottedGeometry.ToPaintGeometry(),
				points, //线条坐标
				bParentEnabled ? ESlateDrawEffect::None : ESlateDrawEffect::DisabledEffect, //在原色基础之上在增加别的效果
				ColorS[0], //显示颜色
				true, //抗锯齿
				3 //线的宽度
			);
		}
	}
	return SCompoundWidget::OnPaint(Args, AllottedGeometry, MyCullingRect, OutDrawElements, LayerId, InWidgetStyle, bParentEnabled);
}

void SRingGraphList::UpdataListData(TMap<FString, int> InListData)
{
	ListData.Empty();
	int AllValue = 0;
	for (auto& item : InListData)
	{
		AllValue += item.Value;
	}
	if(AllValue == 0)
		return;
	for (auto& item : InListData)
	{
		if((float)item.Value == 0.0)
			continue;
		ListData.Add(item.Key, ((float)item.Value / (float)AllValue * 360.0));
	}
	FString ShowValueStr = FString::Printf(TEXT("%dm"), AllValue);
	AllData->SetText(FText::FromString(ShowValueStr));
	UpdataTextInfo(InListData, AllValue);
}

void SRingGraphList::UpdataTextInfo(TMap<FString, int> InListData, int AllValue)
{
	//先卸载之前的字符
	FChildren* Children = ShowInfo->GetChildren();
	for (int i = Children->Num() - 1; i >= 0; i--)
	{
		ShowInfo->RemoveSlot(Children->GetChildAt(i));
	}

	ShowInfo->AddSlot().FillHeight(1)
	[
		SNew(SSpacer)
	];
	int i = 0;
	for (auto& item : InListData)
	{
		FString Baifen = FString::Printf(TEXT("%.1f%%"), (float)item.Value * 100.0 / (float)AllValue);

		FString valueInfo = FString::Printf(TEXT("%dm"), item.Value);
		ShowInfo->AddSlot().AutoHeight()
		[
			SNew(SHorizontalBox)
			+ SHorizontalBox::Slot().AutoWidth()
			                        .HAlign(HAlign_Center)
			                        .VAlign(VAlign_Center)
			[
				SNew(SBox).HeightOverride(8).WidthOverride(8)
				[
					SNew(SColorBlock).Color(ColorS[i])
				]
			]
			+ SHorizontalBox::Slot().Padding(FMargin(16, 0, 0, 0)).VAlign(VAlign_Center).AutoWidth()
			[
				SNew(STextBlock_TextStyleLambda_List)
						.Justification(ETextJustify::Center)
						.Text(FText::FromString(item.Key))
						.TEXTSTYLE("AlibabaRegular_10_FFFFFFD9")
			]
			+ SHorizontalBox::Slot().Padding(FMargin(4, 0)).VAlign(VAlign_Center).AutoWidth()
			[
				SNew(STextBlock_TextStyleLambda_List)
					.Justification(ETextJustify::Center)
					.Text(FText::FromString(Baifen))
					.TEXTSTYLE(TextColor[i])
			]
			+ SHorizontalBox::Slot().VAlign(VAlign_Center).AutoWidth()
			[
				SNew(STextBlock_TextStyleLambda_List)
					.Justification(ETextJustify::Center)
					.Text(FText::FromString(valueInfo))
					.TEXTSTYLE("AlibabaRegular_10_FFFFFFD9")
			]
			+ SHorizontalBox::Slot().FillWidth(1)
			[
				SNew(SSpacer)
			]
		];
		i++;
	}
	ShowInfo->AddSlot().FillHeight(1)
	[
		SNew(SSpacer)
	];
}

void SRingGraphList::CleanList()
{
	UpdataListData(TMap<FString, int>());
}

void SRingGraphList::GetEllipseOuterPoint(float InCenterX, float InCenterY, float Width,float Height, float angle, FVector2D &InPoint) const
{
	float Inside_R = Width * Height / FMath::Sqrt(FMath::Pow(Width * FMath::Sin(angle), 2) + FMath::Pow(Height * FMath::Cos(angle), 2));
	InPoint.X = InCenterX + Inside_R * FMath::Cos(angle);
	InPoint.Y = InCenterY + Inside_R * FMath::Sin(angle);
}

void SRingGraphList::ShowOtherCircle(bool IsShow)
{
	if(IsShow)
	{
		NeiYuan->SetVisibility(EVisibility::Visible);
		WaiYuan->SetVisibility(EVisibility::Visible);
	}
	else
	{
		NeiYuan->SetVisibility(EVisibility::Collapsed);
		WaiYuan->SetVisibility(EVisibility::Collapsed);
	}
}

void SRingGraphList::ShowText(bool IsShow)
{
	if(IsShow)
	{
		AllData->SetVisibility(EVisibility::Visible);
		OtherText->SetVisibility(EVisibility::Visible);
	}
	else
	{
		AllData->SetVisibility(EVisibility::Collapsed);
		OtherText->SetVisibility(EVisibility::Collapsed);
	}
}
